package com.cc.realm;

import com.cc.entity.SysRight;
import com.cc.entity.SysRole;
import com.cc.entity.SysUser;
import com.cc.service.ISysUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 新梦想.陈超
 * @version 2021.2
 * @Description: {TODO}
 * @date 2022/5/26 下午3:01
 */

public class SysUserRealm extends AuthorizingRealm {

    @Resource
    private ISysUserService iSysUserService;
    //TODO 授权---能不能访问某个接口？
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //1.获取认证通过的用户对象
        SysUser primaryPrincipal = (SysUser) principalCollection.getPrimaryPrincipal();
        //2.获取用户对应的角色对象
        SysRole sysRole = primaryPrincipal.getSysRole();
        SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();//根据身份从数据库获取该身份拥有的⻆角⾊色或权限//假设zhang这个⽤用户拥有admin⻆角⾊色，和user:create  user:update menu:createSet<String>roles=newHashSet<>();//⽅方式⼀一：根据⻆角⾊色来授权//        if(userName.equals("zhang")){//            roles.add("admin");//            authorizationInfo.setRoles(roles);//        }else{//            roles.add("guest");//            authorizationInfo.setRoles(roles);//        }//⽅方式⼆二：根据权限Set<String>permitions=newHashSet();if(userName.equals("zhang")){permitions.add("user:create");permitions.add("user:update");permitions.add("menu:create");        }else{permitions.add("user:create");        }authorizationInfo.setStringPermissions(permitions);//log.info("【授权信息为：{}】",authorizationInfo.getRoles().iterator().next());log.info("【授权信息为：{}】",authorizationInfo.getStringPermissions().iterator().next());returnauthorizationInfo;    }/*身份认证*/@OverrideprotectedAuthenticationInfodoGetAuthenticationInfo(AuthenticationTokenauthenticationToken) throwsAuthenticationException {//两个关键：身份   +  证明或凭证//1.身份(登录的⽤用户名)Stringprincipal= (String) authenticationToken.getPrincipal();//身份(⽤用户名)//2.凭证（数据库）Useruser=userMapper.findUserByUserName(principal);log.info("[登录的查询⽤用户信息为：{}]",user);/*要将⽤用户的凭证返回给shiro:⽤用户名，⽤用户加密后的密码，盐，shiro将其缓存起来。下次再登录的时候，shiro根据⽤用户名，明⽂文密码到shiro缓存中匹配⽤用户凭证陈超@15616207640陈超@15616207640陈超@15616207640陈超@15616207640陈超@15616207640陈超@15616207640陈超@15616207640第15/17页

        Set roleSet = new HashSet<String>();
        roleSet.add(sysRole.getRoleName());
        //3.将角色名称放到授权缓存中
        authorizationInfo.setRoles(roleSet);
        return authorizationInfo;
    }
    //TODO 认证---是不是我们用户？
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //TODO 1.获取身份和凭证 (获取用户名和密码查询用户信息)
        String principal = (String) authenticationToken.getPrincipal();
//        String credentials = (String) authenticationToken.getCredentials();
        //TODO 2.查询数据库
        SysUser lgnUser = iSysUserService.queryUserByPrincipal(principal);
        //TODO 3.密码校验
        SimpleAuthenticationInfo authenticationInfo = new 	SimpleAuthenticationInfo(lgnUser,
                lgnUser.getUsrPassword(), 	//密文
                ByteSource.Util.bytes(lgnUser.getPasswordSalt()), //盐
                getName());

        return authenticationInfo;
    }
}
